//
//  SunBankListVC.m
//  Xspcf
//
//  Created by sunshine on 16/11/1.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunBankListVC.h"

@interface SunBankListVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UIScrollView *_scrollView;
    UITableView *_tb;
    NSArray *_dataArr;
}
@end

@implementation SunBankListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择银行";
    self.view.backgroundColor = BACKGROUND;
    [self creatUI];
    [self requestToBank];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)creatUI{
    UIView *msg = [[UIView alloc]init];
    [self.view addSubview:msg];
    [msg lt_Top:SunEdgesMake(0, 0, 0, Y(40)) to:nil save:YES];
    msg.backgroundColor = [HXColor colorWithHexString:@"FAEFBF"];
    UILabel *red = [[UILabel alloc]init];
    [msg addSubview:red];
    [red lt_setSize:CGSizeMake(X(20), X(20)) save:YES];
    [red lt_YcenterTo:nil save:YES];
    [red lt_setLeft:X(15) to:nil save:YES];
    red.clipsToBounds = YES;
    red.backgroundColor = RED;
    red.text = @"!";
    red.font = [UIFont boldSystemFontOfSize:14];
    red.textAlignment = NSTextAlignmentCenter;
    red.textColor = WHITE;
    red.layer.cornerRadius = X(10);
    _scrollView = [[UIScrollView alloc]init];
    [msg addSubview:_scrollView];
    [_scrollView lt_setLeft:X(12) to:red Right:X(10) to:nil save:YES];
    [_scrollView lt_YcenterTo:nil save:YES];
    [_scrollView lt_setHeight:Y(30) save:YES];
    _tb = [[UITableView alloc]initWithFrame:CGRectNull style:UITableViewStylePlain];
    [self.view addSubview:_tb];
    [_tb lt_All:SunEdgesMake(Y(40), 0, 0, 0) save:YES];
    _tb.delegate = self;
    _tb.dataSource = self;
    _tb.showsVerticalScrollIndicator = NO;
}

- (void)requestToBank{
    [[Sun shareSun]showActivity:self];
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_BANK];
    [HttpRequest postURL:url withParam:nil success:^(id requestDic, NSString *msg) {
        [[Sun shareSun]hideActivity];
        NSString *text = [requestDic objectForKey:@"text"];
        [self loadScrollText:text];
        _dataArr = requestDic[@"list"];
        [_tb reloadData];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]hideActivity];
        [self loadScrollText:nil];
        [[Sun shareSun]showMsg:errorInfo];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return Y(70);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        UIImageView *img = [[UIImageView alloc]init];
        [cell addSubview:img];
        [img lt_YcenterTo:nil save:YES];
        [img lt_setLeft:X(20) to:nil save:YES];
        [img lt_setSize:CGSizeMake(X(32), Y(32)) save:YES];
        img.contentMode = UIViewContentModeScaleAspectFit;
        img.tag = 1;
        UILabel *title = [[UILabel alloc]init];
        [cell addSubview:title];
        [title lt_setLeft:X(10) to:img save:YES];
        [title lt_setTop:Y(18) to:nil save:YES];
        title.font = [UIFont systemFontOfSize:16];
        title.textColor = BLACK;
        title.tag = 2;
        UILabel *content = [[UILabel alloc]init];
        [cell addSubview:content];
        [content lt_leftEqual:title save:YES];
        [content lt_setTop:Y(5) to:title save:YES];
        content.textColor = GRAY;
        content.font = [UIFont systemFontOfSize:11];
        content.tag = 3;
//        UILabel *recommand = [[UILabel alloc]init];
//        [cell addSubview:recommand];
//        [recommand lt_YcenterTo:title save:YES];
//        [recommand lt_setLeft:X(36) to:title save:YES];
//        [recommand lt_setSize:CGSizeMake(X(32), Y(16)) save:YES];
//        recommand.layer.borderWidth = 1;
//        recommand.layer.borderColor = RED.CGColor;
//        recommand.layer.cornerRadius = 2;
//        recommand.textColor = RED;
//        recommand.textAlignment = NSTextAlignmentCenter;
//        recommand.font = [UIFont systemFontOfSize:11];
//        recommand.text = @"推荐";
//        recommand.tag = 4;
    }
    UIImageView *img = [cell viewWithTag:1];
    UILabel *title = [cell viewWithTag:2];
    UILabel *content = [cell viewWithTag:3];
//    UILabel *recommand = [cell viewWithTag:4];
    NSDictionary *data = _dataArr[indexPath.row];
    [img sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KIMG,[data objectForKey:@"img"]]]];
    title.text = [data objectForKey:@"name"];
    content.text = [NSString stringWithFormat:@"充值限额:单笔充值%@元，单日充值%@元",[data objectForKey:@"perLimit"],[data objectForKey:@"dayLimit"]];
//    if ([[data objectForKey:@"sign"]intValue] == 1) {
//        recommand.hidden = NO;
//    }else {
//        recommand.hidden = YES;
//    }
    return cell;
}

- (void)getBank:(BankBlock)block{
    self.bankBlock = block;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *data = _dataArr[indexPath.row];
    self.bankBlock(data);
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)loadScrollText:(NSString *)str{
    if (!str) {
        str = @"由于银行对第三方支付有充值限额，为了以后更好的投资，建议您绑定大额银行卡";
    }
    str = [@"  " stringByAppendingString:str];
    CGSize rectsize = CGSizeMake(MAXFLOAT,25);
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:13]};
    CGSize size = [str boundingRectWithSize:rectsize options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    if (size.width <= KScreenW) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenW, 25)];
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:13];
        label.text = str;
        label.textAlignment = NSTextAlignmentCenter;
        [_scrollView addSubview:label];
    }else{
        UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, size.width + 20, 25)];
        label1.textColor = [UIColor blackColor];
        label1.font = [UIFont systemFontOfSize:13];
        label1.text = str;
        [_scrollView addSubview:label1];
        UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(size.width + 20, 0, size.width + 10, 25)];
        label2.textColor = [UIColor blackColor];
        label2.font = [UIFont systemFontOfSize:13];
        label2.text = str;
        [_scrollView addSubview:label2];
        _scrollView.contentSize = CGSizeMake((size.width + 20)*2, 25);
        [self startAnimateWithTime:size.width/30 delay:2 left:size.width + 20 - KScreenW];
    }
}

- (void)startAnimateWithTime:(int)time delay:(int)delay left:(float)left{
    [UIView animateKeyframesWithDuration:time delay:delay options:(UIViewKeyframeAnimationOptions)UIViewAnimationOptionCurveLinear animations:^{
        _scrollView.contentOffset = CGPointMake(_scrollView.contentSize.width - KScreenW, 0);
    } completion:^(BOOL finished) {
        _scrollView.contentOffset = CGPointMake(left, 0);
        [self startAnimateWithTime:time delay:0 left:left];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
